package org.poem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.poem.TaskDetailStatusVO;
import org.poem.entity.TTaskDetail;
import org.poem.entity.TTaskDetailHistory;
import org.poem.enums.EnumTaskDetailStatus;
import org.poem.id.SnowFlake;
import org.poem.mapper.TTaskDetailHistoryMapper;
import org.poem.service.TTaskDetailHistoryService;
import org.poem.service.TTaskDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author poem
 */
@Service
public class TTaskDetailHistoryServiceImpl extends ServiceImpl<TTaskDetailHistoryMapper, TTaskDetailHistory> implements TTaskDetailHistoryService {


    @Autowired
    private TTaskDetailService tTaskDetailService;

    /**
     * 更新装填
     *
     * @param taskDetailStatusVO
     */
    @Override
    public void updateStatus(TaskDetailStatusVO taskDetailStatusVO) {
        QueryWrapper<TTaskDetailHistory> tTaskDetailHistoryQueryWrapper = new QueryWrapper<>();
        tTaskDetailHistoryQueryWrapper.lambda().eq(true, TTaskDetailHistory::getTaskDetailId, taskDetailStatusVO.getTaskDetailId());
        TTaskDetailHistory taskDetailHistory  = new TTaskDetailHistory();
        taskDetailHistory.setCreateTime(LocalDateTime.now());
        taskDetailHistory.setId(SnowFlake.genLongId());
        taskDetailHistory.setHandleSum(0L);
        taskDetailHistory.setProcess(0F);
        taskDetailHistory.setStatus(EnumTaskDetailStatus.NOT_STARTED.getId());
        taskDetailHistory.setTaskDetailId(taskDetailStatusVO.getTaskDetailId());
        this.save(taskDetailHistory);

        taskDetailHistory.setStatus(taskDetailStatusVO.getStatus());
        if (taskDetailHistory.getProcess() != null && taskDetailStatusVO.getProcess() != null) {
            if (taskDetailHistory.getProcess() < taskDetailStatusVO.getProcess()) {
                taskDetailHistory.setProcess(taskDetailStatusVO.getProcess());
            }
        } else {
            taskDetailHistory.setProcess(taskDetailStatusVO.getProcess() == null ? 0F : taskDetailStatusVO.getProcess());
        }
        taskDetailHistory.setHandleSum(taskDetailStatusVO.getHandlerSum());
        taskDetailHistory.setErrorMsg(taskDetailStatusVO.getErrorMsg());
        if (taskDetailStatusVO.getStatus().equals(EnumTaskDetailStatus.RUNNING.getId())) {
            taskDetailHistory.setStartTime(LocalDateTime.now());
        }
        if (taskDetailStatusVO.getStatus().equals(EnumTaskDetailStatus.FAIL.getId())
                || taskDetailStatusVO.getStatus().equals(EnumTaskDetailStatus.SUCCESS.getId())) {
            taskDetailHistory.setEndTime(LocalDateTime.now());
            TTaskDetail tTaskDetail = this.tTaskDetailService.getById(taskDetailStatusVO.getTaskDetailId());
            tTaskDetail.setComplete(1);
            this.tTaskDetailService.updateById(tTaskDetail);
        }
        this.updateById(taskDetailHistory);

    }
}
